package com.avicsafety.ezsystem.mvp.presenter;

import com.avicsafety.ezsystem.mvp.model.MvpTestModel;
import com.avicsafety.ezsystem.mvp.view.MvpTestView;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class MvpTestPresenter extends BasePresenter<MvpTestView> {


    public String TAG = "MvpTestPresenter";

    MvpTestModel model;

    public MvpTestPresenter(){
        model = new Retrofit.Builder().baseUrl("https://ps.haishengshop.com/")
                .build()
                .create(MvpTestModel.class);

    }


    public void getData(String params){
        if(isBindView()) getView().showLoading();
        System.out.println("start");

        Call<ResponseBody> call = model.loadData();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if(isBindView()) getView().closeLoading();
                String data = "";
                try {
                    data = response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if(isBindView()) getView().setData(data);
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                System.out.println("error");
            }
        });




//        AndroidNetworking.get("http://www.avicsafety.com/ip_search.aspx")
//           .build()
//                .getAsString(new StringRequestListener() {
//                    @Override
//                    public void onResponse(String response) {
//                        if(isBindView()) view.closeLoading();
//                        Log.v("============Response:",response);
//                        if(isBindView()) view.setData(response.toString());
//                    }
//
//                    @Override
//                    public void onError(ANError anError) {
//                        if(isBindView()) view.closeLoading();
//                        Log.v("============Error:",anError.getErrorBody());
//
//                    }
//        });
    }
}


